package com.zust.data.web.rest;

import com.zust.data.domain.param.UserLoginParam;
import com.zust.data.domain.param.UserRegParam;
import com.zust.data.domain.param.UserSetBaseParam;
import com.zust.data.domain.param.UserSetPwdParam;
import com.zust.data.dto.ResponseDto;
import com.zust.data.dto.SessionInfo;
import com.zust.data.serivce.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author senselyan
 * @Description: ${todo}
 * @date 2018/4/4 16:26
 */
@RestController
@RequestMapping("/user")
public class UserResource {

    @Inject
    private UserService userService;



    @RequestMapping("/doLogin")
    public ResponseDto login(UserLoginParam userLoginParam,
            HttpServletRequest request, HttpSession session) {
        try {
            return userService.doLogin(userLoginParam, session);
        } catch (Exception e) { e.printStackTrace(); return new ResponseDto(false, "服务器错误，请联系管理员");}
    }

    @RequestMapping("/doReg")
    public ResponseDto reg(UserRegParam userRegParam) {
        try {
            return userService.doRegister(userRegParam);
        } catch (Exception e) { e.printStackTrace(); return new ResponseDto(false, "服务器错误，请联系管理员");}
    }

    @RequestMapping("/setBase")
    public ResponseDto setBase(UserSetBaseParam userSetBaseParam, HttpSession session) {
        SessionInfo sessionInfo = (SessionInfo)session.getAttribute("sessionInfo");
        BeanUtils.copyProperties(userSetBaseParam, sessionInfo);
        session.setAttribute("sessionInfo", sessionInfo);
        return userService.doSetBase(sessionInfo.getId(), userSetBaseParam);
    }
    @RequestMapping("/setIcon")
    public ResponseDto setIcon(@RequestParam("icon") String icon, HttpSession session) {
        SessionInfo sessionInfo = (SessionInfo)session.getAttribute("sessionInfo");
        sessionInfo.setIcon(icon);
        session.setAttribute("sessionInfo", sessionInfo);
        return userService.doSetIcon(sessionInfo.getId(), icon);
    }
    @RequestMapping("/setPwd")
    public ResponseDto setPwd(UserSetPwdParam userSetPwdParam, HttpSession session) {
        try {
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute("sessionInfo");
            return userService.doSetPwd(sessionInfo.getId(), userSetPwdParam);
        } catch (Exception e) { e.printStackTrace(); return new ResponseDto(false, "服务器错误，请联系管理员");}
    }





//
//    @GetMapping("/custom/list")
//    @Secured(MicroserviceAuthoritiesConstants.ADMIN)
//    public ResponseEntity<JdResponse<PageResultVo<CustomBackVo>>> list(CustomListParam customListParam,
//                                                                       @ApiParam @PageableDefault(sort = {"createTime"}, direction = Sort.Direction.DESC) Pageable pageable) throws URISyntaxException {
//        Page<FmCustom> page = customService.list(pageable, customListParam);
//        List<CustomBackVo> list = customRestMapper.entityListToCustomBackVoList(page.getContent());
//
//
//        HttpHeaders headers =
//                PaginationUtil.generatePaginationHttpHeaders(page, "/api/custom/list");
//
//        return new ResponseEntity<>(
//                JdResponse.builder()
//                        .baseResponse(ResultCode.OPERATION_SUCCESS)
//                        .result(list)
//                        .build(),
//                headers,
//                HttpStatus.OK);
//
//    }
}
